package com.ruoyi.mes.controller;

import java.math.BigDecimal;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.mes.domain.MesDispatch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.mes.domain.MesWork;
import com.ruoyi.mes.service.IMesWorkService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 加工单Controller
 * 
 * @author ruoyi
 * @date 2021-12-13
 */
@RestController
@RequestMapping("/mes/work")
public class MesWorkController extends BaseController
{
    @Autowired
    private IMesWorkService mesWorkService;

    /**
     * 查询加工单列表
     */
    @PreAuthorize(hasPermi = "mes:work:list")
    @GetMapping("/list")
    public TableDataInfo list(MesWork mesWork)
    {
        startPage();
        List<MesWork> list = mesWorkService.selectMesWorkList(mesWork);
        return getDataTable(list);
    }

    /**
     * 导出加工单列表
     */
    @PreAuthorize(hasPermi = "mes:work:export")
    @Log(title = "加工单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MesWork mesWork) throws IOException
    {
        List<MesWork> list = mesWorkService.selectMesWorkList(mesWork);
        ExcelUtil<MesWork> util = new ExcelUtil<MesWork>(MesWork.class);
        util.exportExcel(response, list, "加工单数据");
    }

    /**
     * 获取加工单详细信息
     */
    @PreAuthorize(hasPermi = "mes:work:query")
    @GetMapping(value = "/{mesWorkUuid}")
    public AjaxResult getInfo(@PathVariable("mesWorkUuid") String mesWorkUuid)
    {
        return AjaxResult.success(mesWorkService.selectMesWorkByMesWorkUuid(mesWorkUuid));
    }

    /**
     * 新增加工单
     */
    @PreAuthorize(hasPermi = "mes:work:add")
    @Log(title = "加工单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MesWork mesWork)
    {
        // 单据编号查重
        MesWork d = new MesWork();
        d.setDocNum( mesWork.getDocNum() );
        List<MesWork> listByDocNum = mesWorkService.selectMesWorkList(d);
        if(listByDocNum.size()>0){
            return new AjaxResult(1521,"您提交的加工号已存在！请重新填写！");
        }else {
            BigDecimal totalQuota = new BigDecimal("0");
            for (int i = 0; i < mesWork.getMesWorkWorkersList().size(); i++) {
                BigDecimal a = mesWork.getMesWorkWorkersList().get(i).getQuotaWorkingHours();
                totalQuota = totalQuota.add(a);
            }
            if (totalQuota.compareTo(mesWork.getTotalQuotaQtyMin()) <= 0) {
                return toAjax(mesWorkService.insertMesWork(mesWork));
            } else {
                return new AjaxResult(1521, "您提交的员工标准定额总和不能大于总定额！");
            }
//            // 获取当前填写的加工数量
//            BigDecimal curQty = mesWork.getWorkQty();
//            // 获取剩余可填写的加工数量
//            BigDecimal remainQty = mesWorkService.selectRemainWorkQty(mesWork);
//            // 剩余可填写的加工数量和当前填写的加工数量做比较
//            if (curQty.compareTo(remainQty) > 0) {
//                return new AjaxResult(1521, "您提交的派工数量为" + curQty + "," + "当前剩余实际可派工数量为" + remainQty + "!");
//            } else {
//
//            }
        }
    }



    /**
     * 修改加工单
     */
    @PreAuthorize(hasPermi = "mes:work:edit")
    @Log(title = "加工单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MesWork mesWork)
    {
        return toAjax(mesWorkService.updateMesWork(mesWork));
    }

    /**
     * 删除加工单
     */
    @PreAuthorize(hasPermi = "mes:work:remove")
    @Log(title = "加工单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{mesWorkUuids}")
    public AjaxResult remove(@PathVariable String[] mesWorkUuids)
    {
        return toAjax(mesWorkService.deleteMesWorkByMesWorkUuids(mesWorkUuids));
    }
}
